package cn.chenshiwu121.service.impl;

import cn.chenshiwu121.jedis.JedisClient;
import cn.chenshiwu121.mapper.ContentMapper;
import cn.chenshiwu121.pojo.Content;
import cn.chenshiwu121.pojo.ContentExample;
import cn.chenshiwu121.pojo.EasyUIDataGridResult;
import cn.chenshiwu121.service.ContentService;
import cn.chenshiwu121.utils.JsonUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Controller
@Transactional
public class ContentServiceImpl implements ContentService {

    @Value("${CONTENT_KEY}")
    private String CONTENT_KEY;

    @Autowired
    private ContentMapper mapper;

    @Autowired
    private JedisClient client;

    @Override
    public EasyUIDataGridResult<Content> getContentList(Long categoryId, Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        EasyUIDataGridResult<Content> result = new EasyUIDataGridResult<>();
        ContentExample example = new ContentExample();
        ContentExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        List<Content> contents = mapper.selectByExample(example);
        PageInfo<Content> info = new PageInfo<>(contents);
        result.setRows(contents);
        result.setTotal((int)info.getTotal());
        return result;
    }

    @Override
    public void saveContent(Content content) throws Exception {
        int rows = mapper.insertSelective(content);
        try {
            // 内容有修改，删除缓存
            client.hdel(CONTENT_KEY, content.getCategoryId() + "");
        } catch (Exception e) {
            // e.printStackTrace();
        }
        if (rows < 1) throw new Exception("ERROR:save failed");
    }

    @Override
    public void deleteContentByIds(String ids) throws Exception {
        String[] idArr = ids.split(",");
        List<Long> idList = new ArrayList<>();
        for (String id: idArr) {
            idList.add(Long.valueOf(id));
        }
        ContentExample example = new ContentExample();
        example.createCriteria().andIdIn(idList);
        int rows = mapper.deleteByExample(example);
        if (rows < idList.size()) throw new Exception("ERROR:delete failed");
    }

    @Override
    public void editContent(Content content) throws Exception {
        try {
            // 内容有修改，删除缓存
            client.hdel(CONTENT_KEY, content.getCategoryId() + "");
        } catch (Exception e) {
            // e.printStackTrace();
        }
        // 修改数据库
        int rows = mapper.updateByPrimaryKey(content);
        if (rows < 1) throw new Exception("ERROR:edit failed");
    }

    @Override
    public List<Content> getContentListByCatId(Long categoryId) {
        String categoryIdStr = categoryId + "";
        try {
            // 如果有缓存
            String jsonData = client.hget(CONTENT_KEY, categoryIdStr);
            if (StringUtils.isNoneBlank(jsonData))
                return JsonUtils.jsonToList(jsonData, Content.class);
        } catch (Exception e) {
            // e.printStackTrace();
        }
        // 没有缓存，查询数据库
        ContentExample example = new ContentExample();
        example.createCriteria().andCategoryIdEqualTo(categoryId);
        List<Content> contents = mapper.selectByExample(example);
        // 保存缓存
        try {
            client.hset(CONTENT_KEY, categoryIdStr, JsonUtils.objectToJson(contents));
        } catch (Exception e) {
            // e.printStackTrace();
        }
        return contents;
    }
}
